<template>
  <div class="main-container">
    <el-card class="box-card !border-none" shadow="never">
      <div class="flex justify-between items-center">
        <span class="text-lg">{{ pageName }}</span>
      </div>
      <el-card class="box-card !border-none !px-[35px]" shadow="never">
        <el-row class="flex">
            <el-col :span="8" class="min-w-[100px]">
                <div class="statistic-card">
                    <el-statistic :value="memberDividendTable.total_contribution_points" />
                    <div class="statistic-footer">
                        <div class="footer-item text-[14px] text-[#666]">
                          总增加积分
                        </div>
                    </div>
                </div>
            </el-col>
        </el-row>
    </el-card>
      <el-card class="box-card !border-none my-[10px] table-search-wrap" shadow="never">
        <el-form
          :inline="true"
          :model="memberDividendTable.searchParam"
          ref="searchFormRef"
        >
          <el-form-item :label="t('dividendDate')" prop="dividend_date">
            <el-date-picker v-model="memberDividendTable.searchParam.dividend_date" type="datetimerange"
                value-format="YYYY-MM-DD HH:mm:ss" start-placeholder="起始日期"
                end-placeholder="结束日期" />
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="loadMemberDividendList()">{{
              t("search")
            }}</el-button>
            <el-button @click="resetForm(searchFormRef)">{{ t("reset") }}</el-button>
          </el-form-item>
        </el-form>
      </el-card>

      <div class="mt-[10px]">
        <el-table
          :data="memberDividendTable.data"
          size="large"
          v-loading="memberDividendTable.loading"
        >
          <template #empty>
            <span>{{ !memberDividendTable.loading ? t("emptyData") : "" }}</span>
          </template>
          <el-table-column
            prop="member.username"
            :label="t('member')"
            min-width="120"
            :show-overflow-tooltip="true"
          />

          <el-table-column
            prop="contribution"
            :label="t('contribution')"
            min-width="120"
            :show-overflow-tooltip="true"
          />

          <el-table-column
            prop="amount"
            :label="t('amount')"
            min-width="120"
            :show-overflow-tooltip="true"
          />

          <el-table-column :label="t('operation')" fixed="right" min-width="120">
            <template #default="{ row }">
              <el-button type="primary" link @click="deleteEvent(row.id)">{{
                t("delete")
              }}</el-button>
            </template>
          </el-table-column>
          <el-table-column
            prop="dividend_date"
            :label="t('dividendDate')"
            min-width="120"
            :show-overflow-tooltip="true"
          />
        </el-table>
        <div class="mt-[16px] flex justify-end">
          <el-pagination
            v-model:current-page="memberDividendTable.page"
            v-model:page-size="memberDividendTable.limit"
            layout="total, sizes, prev, pager, next, jumper"
            :total="memberDividendTable.total"
            @size-change="loadMemberDividendList()"
            @current-change="loadMemberDividendList"
          />
        </div>
      </div>
    </el-card>
  </div>
</template>

<script lang="ts" setup>
import { reactive, ref } from "vue";
import { t } from "@/lang";
import { getMemberDividendList, deleteMemberDividend, getMemberDividendTotal } from "@/app/api/distributor";
import { ElMessageBox, FormInstance } from "element-plus";
import { useRoute } from "vue-router";
const route = useRoute();
const pageName = route.meta.title;

let memberDividendTable = reactive({
  page: 1,
  limit: 10,
  total: 0,
  loading: true,
  data: [],
  total_contribution_points: 0,
  searchParam: {
    dividend_date: "",
  },
});

const searchFormRef = ref<FormInstance>();

// 字典数据

/**
 * 获取用户分红记录列表
 */
const loadMemberDividendList = (page: number = 1) => {
  memberDividendTable.loading = true;
  memberDividendTable.page = page;

  getMemberDividendList({
    page: memberDividendTable.page,
    limit: memberDividendTable.limit,
    ...memberDividendTable.searchParam,
  })
    .then((res) => {
      memberDividendTable.loading = false;
      memberDividendTable.data = res.data.data;
      memberDividendTable.total = res.data.total;
    })
    .catch(() => {
      memberDividendTable.loading = false;
    });

    getMemberDividendTotal({
      page: memberDividendTable.page,
      limit: memberDividendTable.limit,
      ...memberDividendTable.searchParam,
    }).then((res) => {
      memberDividendTable.total_contribution_points = res.data.total || 0;
    })
};
loadMemberDividendList();
/**
 * 删除用户分红记录
 */
const deleteEvent = (id: number) => {
  ElMessageBox.confirm(t("memberDividendDeleteTips"), t("warning"), {
    confirmButtonText: t("confirm"),
    cancelButtonText: t("cancel"),
    type: "warning",
  }).then(() => {
    deleteMemberDividend(id)
      .then(() => {
        loadMemberDividendList();
      })
      .catch(() => {});
  });
};

const resetForm = (formEl: FormInstance | undefined) => {
  if (!formEl) return;
  formEl.resetFields();
  loadMemberDividendList();
};
</script>

<style lang="scss" scoped>
/* 多行超出隐藏 */
.multi-hidden {
  word-break: break-all;
  text-overflow: ellipsis;
  overflow: hidden;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
}
</style>
